const fs = require("fs");
const path = require('path');
const callfile = require('child_process');
const crypto = require('crypto');
var util = require('util');
function readJson(configPath) {
    let config = fs.readFileSync(configPath, 'utf-8');
    if (config) {
        return JSON.parse(config)
    } else {
        return {}
    }
}
let wujiangConfig = readJson(path.join(__dirname, "../", 'resource', 'WujiangDef.json'))
let dataTypeConfig = [
    ["id", "number"],
    ["name", "string"],
    ["dengjiMoban", "number"],
    ["type", "string"],
    ["jichu", "number"],
    ["shanguangQ", "number"],
    ["shanguangH", "number"],
    ["shanguangcailiao", "number"],
    ["shanxiaohao", "number"],
    ["chongshengID", "number"],
    ["huhuan", "number"],
    ["chaojinhua", "string"],
    ["chaojinhuaHero", "number"],
    ["shifouChaojinhua", "number"],
    ["shifouShanguang", "number"],
    ["parent", "number"],
    ["child", "number"],
    ["jieduan", "number"],
    ["tiaojian", "number"],
    ["silver", "number"],
    ["leijigold", "number"],
    ["materia", "number"],
    ["count", "number"],
    ["leiji", "number"],
    ["rank", "number"],
    ["sssr", "number"],
    ["story", "string"],
    ["photo", "string"],
    ["face", "string"],
    ["avatar", "string"],
    ["hp", "number"],
    ["attack", "number"],
    ["defence", "number"],
    ["speed", "number"],
    ["sgType", "number"],
    ["sgValue", "number"],
    ["skill0", "number"],
    ["skill1", "number"],
    ["canGet", "number"],
    ["exp", "number"],
    ["talent1", "number"],
    ["talent2", "number"],
    ["talent3", "number"],
    ["talent4", "number"],
    ["talent5", "number"],
    ["talent6", "number"],
    ["suofang", "number"],
    ["canhunDefid", "number"],
    ["canhunNum", "number"],
    ["guanzhiMoban", "number"],
    ["chaojinhuaMoban", "number"],
    ["levelMoban", "number"],
    ["artifact", "number"],
    ["jump", "string"],
    ["gerenkeji", "number"],
    ["shenqiEXP", "number"],
    ["paihangjifen", "number"],
    ["chengzhang", "number"],
    ["guanzhiMax", "number"],
    ["zhongzumoban", "number"],
    ["xilianLimit", "number"],
    ["shidai", "number"],
    ["exhibition", "number"],
    ["exhibitionLevel", "number"],
]
let buffer = new ArrayBuffer((1 << 20) * 10);
let view = new DataView(buffer);
let offSet = 0
var lengthConfig = {
    ["Int32"]: 8,
    ["Int64"]: 16,
    // ['setInt16']: 4,
}
console.log("wujiangConfig.length", wujiangConfig.length)
view.setInt32(0, wujiangConfig.length) //数据长度
offSet += lengthConfig.Int32
//先通过一个32位int来表示某一位是否有值
let defaultVueMap = {
    "number": 0,
    'string': '',
}

const encoder = new util.TextEncoder();
const decoder = new util.TextDecoder();
let endodeFunction = {
    "number": (view, vue) => {
        view.setInt32(offSet, vue)
        offSet += lengthConfig.Int32
    },
    'string': (view, vue) => {
        vue = encoder.encode(vue)
        view.setInt32(offSet, vue.length)
        offSet += lengthConfig.Int32
        for (let char of vue) {
            view.setUint8(offSet, char)
            offSet += 1
        }
    },
}
let isFirst = true
function writeObj(view, obj, typeConfig) {
    //写入位置
    let unitLength = Math.ceil(typeConfig.length / 8)
    for (let i = 0; i < unitLength; i++) {
        let signStr = 0
        let arr = []
        for (let j = 0; j < 8; j++) {
            let idx = 8 * i + j
            if (idx < typeConfig.length) {
                let config = typeConfig[idx]
                let sign = obj[config[0]] == defaultVueMap[config[1]] ? 0 : 1
                // signStr[idx] = sign 
                if (j != 0) {
                    signStr = signStr << 1
                }
                signStr = (signStr | sign)
                arr.push(sign)
            }
        }
        // if (isFirst) {
        //     console.log("test set:", signStr)
        // }
        view.setUint8(offSet, signStr)
        // if (isFirst) {
        //     console.log("test get:", view.getUint8(offSet))
        // }
        offSet += 1
    }
    if (isFirst) {
        console.log(buffer)
        isFirst = false
    }
    // let vue = 0
    // for (let config of typeConfig) {
    //     vue = vue << 1 + (obj[config[0]] == defaultVueMap[config[1]] ? 0 : 1)
    // }
    // view.setInt32(offSet, vue)
    // offSet += lengthConfig.Int32
    //写入真实数据
    for (let config of typeConfig) {
        let value = obj[config[0]]
        if (value != defaultVueMap[config[1]]) {
            endodeFunction[config[1]](view, value)
        }
    }
}
for (let obj of wujiangConfig) {
    writeObj(view, obj, dataTypeConfig)
}
let destPath = path.join(__dirname, "../", 'dest', 'WujiangDef.bin')
// let bufOut = new Buffer(offSet);
let viewOut = new DataView(buffer, 0, offSet)
fs.writeFileSync(destPath, viewOut)
console.log("offSet", offSet)
console.log("readLength:", view.getInt32(0))
console.log(new Buffer(buffer))
